package base.stream;

import java.io.*;

public class IOStreamTest01 {
    public static void main(String[] args) {

        inputStreamDemo();

//        outputStreamDemo();

    }

    private static void outputStreamDemo() {
        File file = new File("C:\\Temp\\test\\test.txt");
        try (OutputStream outputStream = new FileOutputStream(file, true)) {

            outputStream.write("append".getBytes());

            outputStream.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    private static void inputStreamDemo() {
        File file = new File("C:\\Temp\\test\\test.txt");

        try (InputStream inputStream = new FileInputStream(file)) {
            int data1 = inputStream.read();
            int data2 = inputStream.read();
            int data3 = inputStream.read();


            System.out.print((char) data1);
            System.out.print((char) data2);
            System.out.println((char) data3);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try (InputStream inputStream = new FileInputStream(file)) {
            byte[] flush = new byte[10240];

            int len = -1;
            while ( (len = inputStream.read(flush) )!= -1) {
                System.out.println(new String(flush, 0, len));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }




    }


}
